package hc.bre.rest.auth

import static org.junit.jupiter.api.Assertions.*

import javax.annotation.PostConstruct

import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.Test
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.boot.test.web.client.TestRestTemplate
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.util.LinkedMultiValueMap
import org.springframework.util.MultiValueMap
import org.springframework.web.client.RestTemplate

@groovy.transform.CompileStatic
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
class AuthRestTest {
	@Autowired
	TestRestTemplate testRestTemplate;

	@org.springframework.boot.web.server.LocalServerPort
	String port;
	String host = ''

//	@PostConstruct
//	void postConstruct()
//	{
//		testRestTemplate = new TestRestTemplate(TestRestTemplate.HttpClientOption.ENABLE_COOKIES)
//		host = 'http://localhost:'+port;
//	}

	boolean _login
//	@Test TEMPXYZ
	void login()
	{
		if(_login)return;
		_login =true;

		Map res = testRestTemplate.postForObject(host+"/auth/login" ,
				[usercode:'admin',userpwd:'999999',rememberMe:true], Map.class);
			
		assert res.error == 0;
		assert res.data['code'] =='admin'
	}

//	@Test TEMPXYZ
	void user() {
		login();
		Map res =  testRestTemplate.getForObject(host+"/auth/user" , Map.class);
		println res;
	}

//	@Test TEMPXYZ
	void getFuncList() {

		login();

		String url = host+"/manage/funcList";
		ResponseEntity<String> response = testRestTemplate.getForEntity(url , String.class);
		assert response.getStatusCode() == HttpStatus.OK;

		println response;
	}
}
